package com.liziba.trend.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.liziba.trend.api.Index;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: trend-parent
 * @Package: com.liziba.trend.service
 * @ClassName: IndexService
 * @Author: liziba
 * @Description:
 * @Date: 2021/2/1 16:54
 * @Version: 1.0
 */
@Service
@CacheConfig(cacheNames = "indexes")
public class IndexService {

    /**
     * 保存基金代码集合
     */
    private List<Index> indexes;
    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(IndexService.class);

    /**
     * 注入http请求RestTemplate
     */
    @Autowired
    private RestTemplate restTemplate;


    /**
     * 如果第三方能用，就把数据放在 redis里
     * 如果第三方不能用，就把断路数据放在 redis里
     * @return
     */
    @HystrixCommand(fallbackMethod = "thirdPartNotConnected")
    @CachePut(key = "'all_codes'")
    public List<Index> fresh() {
        indexes = fetchIndexesFromThirdPart();
        // 第三方数据服务获取数据
        return indexes;
    }

    /**
     * 清空redis中的缓存
     */
    @CacheEvict(allEntries = true)
    public void remove() {}

    /**
     * 获取redis缓存数据
     * @return
     */
    @Cacheable(key = "'all_codes'")
    public List<Index> get() {
        return CollUtil.toList();
    }

    /**
     * 保存数据到redis
     * @return
     */
    @Cacheable(key = "'all_codes'")
    public List<Index> store() {
        return indexes;
    }




    /**
     * 从third-part-index-data-project 获取基金代码数据
     * @return
     */
    public List<Index> fetchIndexesFromThirdPart() {
//        List<Map> tempCodes = restTemplate.getForObject("http://127.0.0.1:8090/indexes/codes.json", List.class);
        List<Map> tempCodes = restTemplate.getForObject("http://127.0.0.1:8090/fundSource/index", List.class);
        return map2Index(tempCodes);
    }

    /**
     * Map装Index
     * @param tempCodes
     * @return
     */
    private List<Index> map2Index(List<Map> tempCodes) {
        List<Index> indexList = new ArrayList<>();
        for (Map tempCode : tempCodes) {
            String code = tempCode.get("code").toString();
            String name = tempCode.get("name").toString();
            Index index = new Index(code, name);
            indexList.add(index);
        }
        return indexList;
    }

    /**
     * 从第三方数据服务接口获取数据失败，触发断路器方法
     * @return
     */
    private List<Index> thirdPartNotConnected() {
        LOGGER.error("断路器触发：thirdPartNotConnected()");
        Index index = new Index();
        index.setCode("000000");
        index.setName("无效指数代码");
        return CollectionUtil.toList(index);
    }



}
